package com.flyco.modules.oms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.modules.oms.entity.OmsOrder;
import com.flyco.modules.oms.entity.OmsOrderItem;
import com.flyco.modules.oms.mapper.OmsOrderItemMapper;
import com.flyco.modules.oms.model.OmsOrderItemExcel;
import com.flyco.modules.oms.service.IOmsOrderItemService;
import org.springframework.stereotype.Service;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * @Description: 订单明细
 * @Author: flyco
 * @Date: 2019-12-10
 * @Version: V1.0
 */
@Service
public class OmsOrderItemServiceImpl extends ServiceImpl<OmsOrderItemMapper, OmsOrderItem> implements IOmsOrderItemService {

    @Autowired
    private OmsOrderItemMapper omsOrderItemMapper;

    @Override
    public List<OmsOrderItem> selectByMainId(String mainId) {
        LambdaQueryWrapper<OmsOrderItem> query = new LambdaQueryWrapper<>();
        query.eq(OmsOrderItem::getOrderId, mainId);
        return this.list(query);
    }

    @Override
    public Integer getFreezeProductCount(Long productId) {
        return omsOrderItemMapper.getFreezeProductCount(productId);
    }

    @Override
    public List<OmsOrderItemExcel> getOrderItemList(OmsOrder order) {

        return omsOrderItemMapper.getOrderItemList(order);
    }

    @Override
    public List<OmsOrderItem> getOrderItemByIds(List<String> orderIds, String productName) {

        LambdaQueryWrapper<OmsOrderItem> query = new LambdaQueryWrapper<>();
        query.in(OmsOrderItem::getOrderId, orderIds);

        if (!StringUtils.isEmpty(productName)) {
            query.like(OmsOrderItem::getProductName, productName);
        }

        return this.list(query);
    }

    @Override
    public OmsOrderItem getByStoreIdAndProductId(Long storeId, Long productId) {
        List<OmsOrderItem> orderItemList = omsOrderItemMapper.getByStoreIdAndProductId(storeId, productId);
        if (CollectionUtils.isEmpty(orderItemList)) {
            return null;
        } else {
            return orderItemList.get(0);
        }
    }

    @Override
    public List<OmsOrderItem> getProductOccupation(String orderId, Long partnerId) {
        return omsOrderItemMapper.getProductOccupation(orderId, partnerId);
    }
}
